using System;
using System.Collections.Generic;
using System.Text;

namespace Acms.DataControls
{
    [System.ComponentModel.Designer(typeof(TopicDataSourceDesigner))]
    [System.Web.UI.ToolboxData("<{0}:TopicDataSource runat=server></{0}:TopicDataSource>")]
    public class TopicDataSource : GenericDataSource<Bll.Topic>
    {
        public enum Fields
        {
            Id,
            SiteId,
            Name,
            Sequence,
            ConcurencyId
        }

        protected override Acms.Bll.Topic PopulateData(UpdateAction action, System.Collections.IDictionary keys, System.Collections.IDictionary values, System.Collections.IDictionary oldValues)
        {
            Bll.Topic tmp = null;
            byte[] concurencyId = null;
            if (action == UpdateAction.Insert)
            {
                tmp = Bll.Topic.CreateInstance();
            }
            else
            {
                int id = GetValue<int>(Fields.Id, keys);
                concurencyId = GetValue<byte[]>(Fields.ConcurencyId, keys);
                tmp = Bll.Topic.GetById(id);
            }

            if (tmp != null)
            {
                tmp.CheckConcurent(concurencyId);

                if (RequiresUpdate<string>(Fields.SiteId, values, oldValues))
                {
                    tmp.SiteId = GetValue<int>(Fields.SiteId, values);
                }

                if (RequiresUpdate<string>(Fields.Name, values, oldValues))
                {
                    tmp.Name = GetValue<string>(Fields.Name, values);
                }

                if (RequiresUpdate<int>(Fields.Sequence, values, oldValues))
                {
                    tmp.Sequence = GetValue<int>(Fields.Sequence, values);
                }
            }

            return tmp;
        }

        protected override int SaveChanges(Acms.Bll.Topic value)
        {
            value.Update();
            return 1;
        }

        protected override void AddInsertedKey(Acms.Bll.Topic item, System.Collections.IDictionary values)
        {
            values[Fields.Id.ToString()] = item.Id;
        }

        protected override int Delete(Acms.Bll.Topic item)
        {
            item.Delete();
            return 1;
        } 
    }

    public class TopicDataSourceDesigner : GenericDataSourceDesigner<Bll.Topic>
    {

        protected override Acms.Bll.Topic GetDesignTimeItem(int index)
        {
            Bll.Topic tmp = Bll.Topic.CreateInstance();
            tmp.Name = string.Format("Topic {0}", index.ToString());
            tmp.SiteId = 1;
            return tmp;
        }
    }
}
